#include "gtsort.h"

errno_t sort(
	void *base, \
	size_t num, \
	size_t size, \
	int(*comp)(const void*, const void*, void*), \
	void *ctx)
{
	if (base == NULL || num == 0 \
		|| size == 0 || comp == NULL)
		return 0;

	void *context = NULL;
	errno_t error;
	for (size_t i = 0, j, k, counter = num - 1; i < counter; ++i)
	{
		for (j = i + 1, k = i; j < num; ++j)
			if (comp(address(base, k, size), \
				address(base, j, size), ctx) > 0)
				k = j;

		error = swap(address(base, i, size), \
			address(base, k, size), size, &context);
		if (error != 0)
			return error;
	}
	swap(NULL, NULL, 0, &context);
	return 0;
}
